package com.ocom.ugcapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.WxServiceUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StaticCodePayUtil;
import com.ocom.common.vo.ugcapi.StaticCodePayVo;
import com.ocom.ugcapi.client.PayConfigClientService;
import com.ocom.ugcapi.client.PayHandleClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2023/3/7
 **/
@Slf4j
@RestController
public class StaticCodeController {


	@Autowired
	private PayConfigClientService payConfigClientService;

	@Autowired
	private PayHandleClientService payHandleClientService;

	@PostMapping(WxServiceUrl.URL_getCodeInfo)
	public Result getCodeInfo(@RequestBody Map<String, String> map) {
		String code = map.get("code");
		StaticCodePayVo staticCodePayVo = StaticCodePayUtil.getCodeInfo(code);
		if (staticCodePayVo == null) {
			return ResultUtil.error(ResultEnum.Err_3072);
		}
		if(staticCodePayVo.getType()!=1){
			return ResultUtil.error(ResultEnum.Err_3073);
		}
		Map<String, Object> resMap = new HashMap<>();
		resMap.put("comId", staticCodePayVo.getComId());
		if(staticCodePayVo.getType()==1){
			resMap.put("code", staticCodePayVo.getDeviceNo());
			resMap.put("deviceNo", staticCodePayVo.getDeviceNo());
		}else{
			resMap.put("code", staticCodePayVo.getCodeId());
			resMap.put("deviceNo", "");
		}
		resMap.put("appId", "");
		resMap.put("merchId", 0l);
		resMap.put("merchName", "");
		resMap.put("appName", "");
		resMap.put("companyName", "");
		resMap.put("deviceName", "");

//		Result result2 = payHandleClientService.getUsefuleMerchPayInfo(staticCodePayVo.getComId(),staticCodePayVo.getDeviceNo(),1);
//		log.info("获取code配置 【{}】【{}】" ,staticCodePayVo,result2);
		//		if (result2.getCode() == 200) {
//			EntiyXfMerchPayInfo entiyXfMerchPayInfo = JSONObject.parseObject(JSONObject.toJSONString(result2.getData()),EntiyXfMerchPayInfo.class);
//			Long merchId = entiyXfMerchPayInfo.getMerchId().longValue();
			Result result = payConfigClientService.getWxInfo(staticCodePayVo.getComId(), 0l);
			if (result.getCode() == 200) {
				JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(result.getData()));
				resMap.put("appId", jsonObject.getString("appid"));
				resMap.put("appName", jsonObject.getString("appName"));
			}
//		}else{
//			return result2;
//		}

		//获取商户名称

		try {
			Result result1 = payHandleClientService.GetMerchInfoByMachNo(staticCodePayVo.getComId(),staticCodePayVo.getDeviceNo());
			if(result1.getCode()==200){
				JSONObject jsonObject1 = JSON.parseObject(JSON.toJSONString(result1.getData()));
				resMap.put("merchId", jsonObject1.getString("merchId"));
				resMap.put("merchName", jsonObject1.getString("merchName"));
				resMap.put("companyName", jsonObject1.getString("companyName"));
				resMap.put("deviceName", jsonObject1.getString("deviceName"));
			}
		}catch (Exception e){
			log.info("查机器商户异常 {{}}",e);
		}
		log.info("查静态码机器商户 {{}}，{{}}",map,resMap);
		return ResultUtil.success(resMap);
	}

	public static void main(String[] args) {
		String rt = StaticCodePayUtil.getCode(1, 74l, "66133289");

		System.out.println(rt);
//		43FFF016E45BF3622293F3518F069198

		String code = "XAK_00000047_01_008_12345678_D7854D27D7BF099CEA0301E97D9F83F1";
		StaticCodePayVo re = StaticCodePayUtil.getCodeInfo(code);

		System.out.println(re);
	}
}
